home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / FAQSYS18.ZIP / FAQS.DAT / VOXEL.TXT / text0001.txt < prev   
Encoding:
Text File  |  1994-08-09  |  7.3 KB  |  173 lines

  1. In article <2ku50l$9lo@access1.digex.net> kmccorm@access1.digex.net (Kevin McCormick) writes:
  2. >Now I've pondered quite a bit how it's done.
  3.  
  4.     Why bother pondering, since the person who created the demo was
  5.     kind enough to post a description of the mothod, too. He did
  6.     this at least in the 'rec.games.programming' and 'comp.graphics.
  7.     algorithms' newsgroups. I think it might be a good idea for all
  8.     the readers of this conference to visit those two, too (at least
  9.     I do that).
  10.  
  11.     In any case, here's the description. I hope Tim Clarke don't mind...
  12.  
  13.     Patrick Aalto
  14.     ap@jyu.fi
  15.  
  16. --------
  17.  
  18.         Voxel landscapes and How I did it
  19.         ---------------------------------
  20.  
  21.  This document describes the method I used in my demo of a Martian terrain,
  22. which can be found at garbo.uwasa.fi:/pc/demo/mars10.zip.
  23.  It's similar to a floating horizon hidden line removal algorithm, so you'll
  24. find discussion of the salient points in many computer graphics books. The
  25. difference is the vertical line interpolation.
  26.  
  27.  
  28. First, some general points:
  29. ---------------------------
  30.  
  31.  The map is a 256x256 grid of points, each having and 8-bit integer height
  32. and a colour. The map wraps round such that, calling w(u,v) the height at
  33. (u,v), then w(0,0)=w(256,0)=w(0,256)=w(256,256). w(1,1)=w(257,257), etc.
  34.  
  35.  Map co-ords: (u,v) co-ordinates that describe a position on the map. The
  36. map can be thought of as a height function w=f(u,v) sampled discretely.
  37.  
  38.  Screen co-ords: (x,y) co-ordinates for a pixel on the screen.
  39.  
  40.  
  41. To generate the map:
  42. --------------------
  43.  
  44.  This is a recursive subdivision, or plasma, fractal. You start of with
  45. a random height at (0,0) and therefore also at (256,0), (0,256), (256,256).
  46. Call a routine that takes as input the size and position of a square, in the
  47. first case the entire map.
  48.  This routine get the heights from the corners of the square it gets given.
  49. Across each edge (if the map has not been written to at the point halfway
  50. along that edge), it takes the average of the heights of the 2 corners on that
  51. edge, applies some noise proportional to the length of the edge, and writes
  52. the result into the map at a position halfway along the edge. The centre of
  53. the square is the average of the four corners+noise.
  54.  The routine then calls itself recursively, splitting each square into four
  55. quadrants, calling itself for each quadrant until the length of the side is
  56. 2 pixels.
  57.  This is probably old-hat to many people, but the map is made more realistic
  58. by blurring:
  59.  
  60.      w(u,v)=k1*w(u,v)+k2*w(u+3,v-2)+k3*w(u-2,v+4) or something.
  61.  
  62.  Choose k1,k2,k3 such that k1+k2+k3=1. The points at which the map is sampled
  63. for the blurring filter do not really matter - they give different effects,
  64. and you don't need any theoretical reason to choose one lot as long as it
  65. looks good. Of course do everything in fixed point integer arithmetic.
  66.  The colours are done so that the sun is on the horizon to the East:
  67.  
  68.      Colour=A*[ w(u+1,v)-w(u,v) ]+B
  69.  
  70. with A and B chosen so that the full range of the palette is used.
  71.  The sky is a similar fractal but without the colour transformation.
  72.  
  73.  
  74. How to draw each frame
  75. ----------------------
  76.  
  77.  First, draw the sky, and blank off about 50 or so scan lines below the
  78. horizon since the routine may not write to all of them (eg. if you are on top
  79. of a high mountain looking onto a flat plane, the plane will not go to the
  80. horizon).
  81.  Now, down to business. The screen is as follows:
  82.  
  83.      ---------------------------
  84.      |                         |
  85.      |                         |
  86.      |           Sky           |
  87.      |                         |
  88.      |                         |
  89.      |a------------------------| Horizon
  90.      |                         |
  91.      |                         |    Point (a)=screen co-ords (0,0)
  92.      |          Ground         |     x increases horizontally
  93.      |                         |     y increases downwards
  94.      |                         |
  95.      ---------------------------
  96.  
  97.  Imagine the viewpoint is at a position (p,q,r) where (p,q) are the (u,v)
  98. map co-ordinates and r is the altitude. Now, for each horizontal (constant v)
  99. line of map from v=r+100 (say) down to v=r, do this:
  100.  
  101.     1. Calculate the y co-ordinate of map co-ord (p,v,0) (perspective transform)
  102.  
  103.     2. Calculate scale factor f which is how many screen pixels high a mountain
  104. of constant height would be if at distance v from q. Therefore, f is small
  105. for map co-ords far away (v>>r) and gets bigger as v comes down towards r.
  106.  
  107.     3. Work out the map u co-ord corresponding to (0,y). v is constant along
  108. each line.
  109.  
  110.     4. Starting at the calculated (u,v), traverse the screen, incrementing the
  111. x co-ordinate and adding on a constant, c, to u such that (u+c,v) are the map
  112. co-ords corresponding to the screen co-ords (1,y). You then have 256 map
  113. co-ords along a line of constant v. Get the height, w, at each map co-ord and
  114. draw a spot at (x,y-w*f) for all x.
  115.  
  116.  Sorry, but that probably doesn't make much sense. Here's an example:
  117. Imagine sometime in the middle of drawing the frame, everything behind a
  118. point (say v=q+50) will have been drawn:
  119.  
  120.          ---------------------------
  121.          |                         |
  122.          |                         |
  123.          |                         |
  124.          |           ****          |
  125.          |        *********        | <- A mountain half-drawn.
  126.          |-----**************------|
  127.          |*************************|
  128.          |*********       *********|
  129.          |******             ******|
  130.          |.........................| <- The row of dots is at screen co-ord y
  131.          |                         |   corresponding to an altitude of 0 for that
  132.          ---------------------------   particular distance v.
  133.  
  134.  Now the screen-scanning routine will get called for v=q+50. It draws in a
  135. point for every x corresponding to heights at map positions (u,v) where u
  136. goes from p-something to p+something, v constant. The routine would put points
  137. at these positions: (ignoring what was there before)
  138.  
  139.      ---------------------------
  140.      |                         |
  141.      |                         |
  142.      |                         |
  143.      |                         |
  144.      |                         |
  145.      |-------------------------|
  146.      |          *****          |
  147.      |       ***     ***       |
  148.      |*******           *******|
  149.      |.........................|
  150.      |                         |
  151.      ---------------------------
  152.  
  153.  So, you can see that the screen gets drawn from the back, one vertical
  154. section after another. In fact, there's more to it than drawing one pixel
  155. at every x during the scan - you need to draw a vertical line between
  156. (x,y old) to (x,y new), so you have to have a buffer containing the y values
  157. for every x that were calculated in the previous pass. You interpolate
  158. along this line (Gouraud style) from the old colour to the new colour also,
  159. so you have to keep a buffer of the colours done in the last pass.
  160.  Only draw the vertical lines if they are visible (ie. going down,
  161. y new>y old). The screen is drawn from the back so that objects can be drawn
  162. inbetween drawing each vertical section at the appropriate time.
  163.  
  164.  If you need further information or details, mail me or post here... Posting
  165. will allow others to benefit from your points and my replies, though.
  166.  
  167.  Thank you for the response I have received since uploading this program.
  168.  
  169.  Tim Clarke, tjc1005@hermes.cam.ac.uk
  170.  
  171.  
  172.  
  173.